<?php 
/**
*	file: ProcessIntake2.php
*	path: /webroot/actions/
*	notes:   
*		this form was updated with VAIL customization on 10-16-08.
*		several witness fields were added.
*		
*/
	//FOR DEBUGGING--comment out when done!
	ini_set('display_errors', 1);
	ini_set('error_reporting', E_ALL ^ E_NOTICE);
	error_reporting(1);

	session_start();
	
	include('../includes/db_connect.php'); 
	//include('../includes/phpheader.php');
	
	$id = $_POST["id"];
	$isGuardian = $_POST["isGuardian"];
	
	################################################
	$isGuardianshipDocumented = $_POST["isGuardianshipDocumented"];
	$legalStatusID= $_POST["legalStatus"];
	//echo"<h1>".$isGuardianshipDocumented."</h1>";
	//echo"<h1>".$legalStatusID."</h1>";	
	//die();
	################################################
	
	$gName = $_POST["guardianName"];
	$gRelationship = $_POST["guardianRelationship"];
	$gHomePhone = $_POST["guardianHomePhone"];
	$gWorkPhone = $_POST["guardianWorkPhone"];
	$gAddress = $_POST["guardianAddress"];
	$gCity = $_POST["guardianCity"];
	$gStateId = $_POST["guardianState"];
	$gZip = $_POST["guardianZip"];
	
	##################################################
	$gCounty = $_POST["guardianCounty"];
	//echo"<h1>".$gCounty."</h1>";	
	//die();
	##################################################
	
	
	$appSig = $_POST["applicantSig"];
	$appSigDateMonth = $_POST["applicantSigDateMonth"];
	$appSigDateDay = $_POST["applicantSigDateDay"];
	$appSigDateYear = $_POST["applicantSigDateYear"];
	$appDate = $appSigDateYear."-".$appSigDateMonth."-".$appSigDateDay;

	$gSig = $_POST["guardianSig"];
	$gSigDateMonth = $_POST["guardianSigDateMonth"];
	$gSigDateDay = $_POST["guardianSigDateDay"];
	$gSigDateYear = $_POST["guardianSigDateYear"];
	$gDate = $gSigDateYear."-".$gSigDateMonth."-".$gSigDateDay;

	
	##########################################################
	$wSig = $_POST["witnessSig"];
	$wSigDateMonth = $_POST["witnessSigDateMonth"];
	$wSigDateDay = $_POST["witnessSigDateDay"];
	$wSigDateYear = $_POST["witnessSigDateYear"];
	$wDate = $wSigDateYear."-".$wSigDateMonth."-".$wSigDateDay;
	
	/*echo"<h1>".$wSig."</h1>";
	echo"<h1>".$wSigDateMonth."</h1>";
	echo"<h1>".$wSigDateDay."</h1>";
	echo"<h1>".$wSigDateYear."</h1>";
	echo"<h1>".$wDate."</h1>";
	die();*/
	###########################################################
	
	
	$sSig = $_POST["staffSig"];
	$sSigDateMonth = $_POST["staffSigDateMonth"];
	$sSigDateDay = $_POST["staffSigDateDay"];
	$sSigDateYear = $_POST["staffSigDateYear"];
	$sDate = $sSigDateYear."-".$sSigDateMonth."-".$sSigDateDay;

	$appGrievanceSig = $_POST["applicantGrievanceSig"];
	$appGrievanceSigDateMonth = $_POST["applicantGrievanceSigDateMonth"];
	$appGrievanceSigDateDay = $_POST["applicantGrievanceSigDateDay"];
	$appGrievanceSigDateYear = $_POST["applicantGrievanceSigDateYear"];
	$appGrievanceDate = $appGrievanceSigDateYear."-".$appGrievanceSigDateMonth."-".$appGrievanceSigDateDay;

	$gGrievanceSig = $_POST["guardianGrievanceSig"];
	$gGrievanceSigDateMonth = $_POST["guardianGrievanceSigDateMonth"];
	$gGrievanceSigDateDay = $_POST["guardianGrievanceSigDateDay"];
	$gGrievanceSigDateYear = $_POST["guardianGrievanceSigDateYear"];
	$gGrievanceDate = $gGrievanceSigDateYear."-".$gGrievanceSigDateMonth."-".$gGrievanceSigDateDay;

	##################################################################
	$wGrievanceSig = $_POST["witnessGrievanceSig"];
	$wGrievanceSigDateMonth = $_POST["witnessGrievanceSigDateMonth"];
	$wGrievanceSigDateDay = $_POST["witnessGrievanceSigDateDay"];
	$wGrievanceSigDateYear = $_POST["witnessGrievanceSigDateYear"];
	$wGrievanceDate = $wGrievanceSigDateYear."-".$wGrievanceSigDateMonth."-".$wGrievanceSigDateDay;
	/*echo"<h1>".$wGrievanceSig."</h1>";
	echo"<h1>".$wGrievanceSigDateMonth."</h1>";
	echo"<h1>".$wGrievanceSigDateDay."</h1>";
	echo"<h1>".$wGrievanceSigDateYear."</h1>";
	echo"<h1>".$wGrievanceDate."</h1>";
	die();*/
	##################################################################
	
	$sGrievanceSig = $_POST["staffGrievanceSig"];
	$sGrievanceSigDateMonth = $_POST["staffGrievanceSigDateMonth"];
	$sGrievanceSigDateDay = $_POST["staffGrievanceSigDateDay"];
	$sGrievanceSigDateYear = $_POST["staffGrievanceSigDateYear"];
	$sGrievanceDate = $sGrievanceSigDateYear."-".$sGrievanceSigDateMonth."-".$sGrievanceSigDateDay;


	
$query = "SELECT id 
			FROM intake2 
			WHERE consumer_id = ".$id;

if(mysql_fetch_array(mysql_query($query))) {
	$query = "UPDATE intake2 
			     SET is_guardian = ".$isGuardian.", 
					is_guardianship_documented = ".$isGuardianshipDocumented.", 
					legal_status_id = ".$legalStatusID.", 
					guardian_name = '".$gName."', 
					guardian_relationship = '".$gRelationship."', 
					guardian_address ='".$gAddress."', 
					guardian_city = '".$gCity."', 
					guardian_state_id = ".$gStateId.", 
					guardian_zip = '".$gZip."', 
					guardian_county = '".$gCounty."', 
					guardian_home_phone = '".$gHomePhone."', 
					guardian_work_phone='".$gWorkPhone."', 
					applicant_signature = '".$appSig."', 
					applicant_signature_date= '".$appDate."', 
					guardian_signature = '".$gSig."', 
					guardian_signature_date= '".$gDate."', 
					witness_signature = '".$wSig."', 
					witness_signature_date= '".$wDate."', 
					staff_signature = '".$sSig."', 
					staff_signature_date = '".$sDate."', 
					applicant_grievance_signature = '".$appGrievanceSig."', 
					applicant_grievance_signature_date= '".$appGrievanceDate."',
					guardian_grievance_signature = '".$gGrievanceSig."', 
					guardian_grievance_signature_date= '".$gGrievanceDate."', 
					witness_grievance_signature = '".$wGrievanceSig."', 
					witness_grievance_signature_date= '".$wGrievanceDate."', 
					staff_grievance_signature = '".$sGrievanceSig."', 
					staff_grievance_signature_date = '".$sGrievanceDate."' 
					WHERE consumer_id = ".$id;
} else {
	$query = "INSERT INTO intake2 (consumer_id, 
										is_guardian, 
										is_guardianship_documented, 
										legal_status_id,
										guardian_name, 
										guardian_relationship, 
										guardian_address,
										guardian_city, 
										guardian_state_id, 
										guardian_zip, 
										guardian_county, 
										guardian_home_phone, 
										guardian_work_phone, 
										applicant_signature, 
										applicant_signature_date, 
										guardian_signature, 
										guardian_signature_date, 
										witness_signature, 
										witness_signature_date, 
										staff_signature, 
										staff_signature_date, 
										applicant_grievance_signature, 
										applicant_grievance_signature_date, 
										guardian_grievance_signature, 
										guardian_grievance_signature_date, 
										witness_grievance_signature, 
										witness_grievance_signature_date, 
										staff_grievance_signature, 
										staff_grievance_signature_date) 
				VALUES (".$id.", 
						   ".$isGuardian.", 
						   ".$isGuardianshipDocumented.",
						   ".$legalStatusID.", 						   
						   '".$gName."', 
						   '".$gRelationship."', 
						   '".$gAddress."', 
						   '".$gCity."', 
						   ".$gStateId.", 
						   '".$gZip."', 
						   '".$gCounty."', 
						   '".$gHomePhone."', 
						   '".$gWorkPhone."', 
						   '".$appSig."', 
						   '".$appSigDate."', 
						   '".$gSig."', 
						   '".$gDate."', 
						   '".$wSig."', 
						   '".$wDate."', 
						   '".$sSig."', 
						   '".$sDate."', 
						   '".$appGrievanceSig."', 
						   '".$appGrievanceSigDate."', 
						   '".$gGrievanceSig."', 
						   '".$gGrievanceDate."', 
						   '".$wGrievanceSig."', 
						   '".$wGrievanceDate."', 
						   '".$sGrievanceSig."', 
						   '".$sGrievanceDate."');";
}

if(mysql_query($query)) {
	//echo "Success <br/>".$query;
	header("Location:../admin.php?action=cieedit&id=".$id."&Status=Intake 2 Form Submitted Successfully");
	exit();
	//echo "Success <br/>".$query;
} else {
	//echo "Failure <br/>".$query.mysql_error();
	header("Location:../admin.php?action=cieedit&id=".$id."&Error=Unable to process Intake 2 Form due to system error.  Please contact system administrator.");
	exit();
	//echo "Failure <br/>".$query;
}


